package com.viphuli.app.tool.handler;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.ButterKnife;

import com.loopj.android.http.RequestParams;
import com.mobeta.android.dslv.DragSortController;
import com.mobeta.android.dslv.DragSortListView;
import com.offroader.utils.DateUtils;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.adapter.ArrangeWeekViewAdapter;
import com.viphuli.app.tool.bean.page.ArrangeViewWeekPage;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.bean.part.ArrangeType;
import com.viphuli.app.tool.bean.part.ArrangeView;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.fragment.ArrangeViewWeekFragment;
import com.viphuli.app.tool.http.ApiRequest;

public class ArrangeWeekViewResponseHandler extends MyBaseHttpResponseHandler<ArrangeViewWeekFragment, ArrangeViewWeekPage> {

	//	View curCheck = null;

	private ArrangeWeekViewAdapter adapter;

	/**
	 * 拖动监听器
	 */
	private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {

		@Override
		public void drop(int from, int to) {

			if (from != to) {

				//移动元素位置
				ArrangeView itemFrom = adapter.getItem(from);
				ArrangeView itemTo = adapter.getItem(to);
				adapter.remove(itemFrom);
				adapter.insert(itemFrom, to);

				//执行排序请求
				AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(caller.getActivity());
				if (token.isLogin()) {

					RequestParams params = new RequestParams();
					params.put("uid", token.getOpenId());
					params.put("to_uid", itemFrom.getUser().getUid());
					params.put("position", itemTo.getUser().getPosition());
					ApiRequest.arrangePositionExchange.request(caller, params, HandlerType.waitting, false);

				}

			}

		}
	};

	@Override
	public void deal() {

		long time = page.time();

		//		caller.getViewWeekLayout().removeAllViews();
		caller.setArrangeTypeList(page.getArrangeTypeList());

		//备注信息
		if (StringUtils.isNotBlank(page.getRemark()))
			caller.getWeekRemark().setText(page.getRemark());

		//初始周视图
		initViewWeekHeader(time);
		initViewWeekRows(page.getArrangeViewList(), time);

		//备注信息
		if (caller.getWeekRemark() != null)
			caller.getWeekRemark().setText(page.getRemark());

	}

	private void initViewWeekHeader(long time) {

		//创建头
		ViewGroup header = ViewUtils.inflateView(caller.getActivity(), R.layout.include_week_view_header);

		//设置头
		Date date = new Date(time);
		List<Date> dateList = DateUtils.dateToWeek(date);

		for (int i = 0; i < dateList.size(); i++) {

			View curDayView = header.getChildAt(i + 1);//路过空白

			TextView nameText = ButterKnife.findById(curDayView, R.id.id_view_week_header_name);
			TextView dateText = ButterKnife.findById(curDayView, R.id.id_view_week_header_date);

			Date curDate = dateList.get(i);

			String[] weekDays = caller.getResources().getStringArray(R.array.week_days);
			String name = DateUtils.getWeekOfDate(curDate, weekDays);
			SimpleDateFormat dayFormat = new SimpleDateFormat("MM-dd");
			String dateStr = dayFormat.format(curDate);

			//设置周日期
			nameText.setText(name);
			dateText.setText(dateStr);

		}

		//添加头
		caller.getViewWeekHeaderLayout().removeAllViews();
		caller.getViewWeekHeaderLayout().addView(header);
	}

	private void initViewWeekRows(List<ArrangeView> arrangeViewList, long time) {

		//排序
		Collections.sort(arrangeViewList, new Comparator<ArrangeView>() {

			@Override
			public int compare(ArrangeView obj1, ArrangeView obj2) {

				int position1 = obj1.getUser().getPosition();
				int position2 = obj2.getUser().getPosition();

				return position1 > position2 ? 1 : (position1 < position2 ? -1 : 0);
			}
		});

		DragSortController mController = new DragSortController(caller.getListView());
		mController.setDragInitMode(DragSortController.ON_LONG_PRESS);//长按拖动

		caller.getListView().setFloatViewManager(mController);
		caller.getListView().setOnTouchListener(mController);
		caller.getArrangeViewList().clear();
		caller.getArrangeViewList().addAll(arrangeViewList);

		adapter = new ArrangeWeekViewAdapter(caller, arrangeViewList, time);
		caller.getListView().setAdapter(adapter);

		//拖动监听
		caller.getListView().setDropListener(onDrop);

	}

	public static final class DayUserArrangeHolder {
		AccountUser user;
		List<ArrangeType> arrangeTypeList;
		Date date;
		private int check = -1;

		public DayUserArrangeHolder(AccountUser user, List<ArrangeType> arrangeTypeList, Date date) {
			this.user = user;
			this.arrangeTypeList = arrangeTypeList;
			this.date = date;
		}

		public AccountUser getUser() {
			return user;
		}

		public void setUser(AccountUser user) {
			this.user = user;
		}

		public List<ArrangeType> getArrangeTypeList() {
			return arrangeTypeList;
		}

		public void setArrangeTypeList(List<ArrangeType> arrangeTypeList) {
			this.arrangeTypeList = arrangeTypeList;
		}

		public Date getDate() {
			return date;
		}

		public void setDate(Date date) {
			this.date = date;
		}

		public int getCheck() {
			return check;
		}

		public void setCheck(int check) {
			this.check = check;
		}

	}

}
